let db = require('../db/dbmanager');
let networkUtils = require('../utils/networkUtils');

/**
 * 设置信息
 */
let Setting={
    cache:[],
    /**
     * @param {*} newData 保存数据
     */
    save:function(newData){
        let that = this;
        that.cache = null;

        return new Promise(async function (resolve) {
            const dbData = await that.load();
            if (dbData) {
                await that.update(newData);
            } else {
                await that.insert(newData);
            }
            resolve(newData);
        });
    },
    /**
     * 插入数据,只给save调用
     */
    insert:function(newData){
        let sqlData={
            $upper_host     : newData.upper_host,
            $upper_port     : newData.upper_port,
            $upper_realm    : newData.upper_realm,
            $upper_password : newData.upper_password,
            $keep_alive_second     : newData.keep_alive_second,
            $upper_account  : newData.upper_account,
            $self_account   : newData.self_account,
            $in_use         : newData.in_use,
            $self_port      : newData.self_port,
            $self_tls_port  : newData.self_tls_port,
            $upper_tcp      : newData.upper_tcp,
            $upper_udp      : newData.upper_udp
        };
        return new Promise(function (resolve) {
            const sql = "insert into UPWARDS(upper_host,upper_port,upper_realm,upper_password,keep_alive_second,upper_account,self_account,in_use,self_port,self_tls_port,upper_tcp,upper_udp"
                +
                ") values(" +
                "$upper_host,$upper_port,$upper_realm,$upper_password,$keep_alive_second,$upper_account,$self_account,$in_use,$self_port,$self_tls_port,$upper_tcp,$upper_udp)";
            db.sqliteDB.run(sql, sqlData);
            resolve(newData);
        });
    },
    /**
     * 更新，只给save调用
     */
    update:function(newData){
        let that=this;
        let sqlData={
            $upper_host  : newData.upper_host,
            $upper_port : newData.upper_port,
            $upper_realm     : newData.upper_realm,
            $upper_password         : newData.upper_password,
            $keep_alive_second     : newData.keep_alive_second,
            $upper_account: newData.upper_account,
            $self_account : newData.self_account,
            $in_use     : newData.in_use,
            $self_port: newData.self_port,
            $self_tls_port: newData.self_tls_port,
            $upper_tcp: newData.upper_tcp,
            $upper_udp: newData.upper_udp
        };
        return new Promise(function (resolve) {
            const sql = "update UPWARDS set " +
                "upper_host = $upper_host,upper_port= $upper_port,upper_realm=$upper_realm,upper_password=$upper_password,keep_alive_second=$keep_alive_second" +
                ",upper_account=$upper_account,self_account=$self_account,in_use=$in_use"+
                ",self_port=$self_port,self_tls_port=$self_tls_port,upper_tcp=$upper_tcp,upper_udp=$upper_udp"+
                " where id=1";
            db.sqliteDB.run(sql, sqlData);
            that.cache = newData
            resolve(newData);
        });
    },
    /**
     * 加载设置
     */
    load:function(){
        let that=this;
        return new Promise(function (resolve) {
            const sql = "SELECT * FROM UPWARDS";
            db.sqliteDB.queryData(sql, async (data) => {
                if (!data || data.length === 0) {
                    let newData = {
                        upper_host: networkUtils.getLocalIP(),
                        upper_port: 5060,
                        upper_realm: networkUtils.getLocalIP(),
                        upper_password: 5060,
                        keep_alive_second: 60,
                        upper_account: '41010500002000000001',
                        self_account: '41010500002000000005',
                        in_use: false,
                        self_port: 5060,
                        self_tls_port: 5061,
                        upper_tcp: true,
                        upper_udp: true
                    };
                    await that.insert(newData)
                    that.cache = [newData];
                    resolve([newData]);
                } else {
                    that.cache = data;
                    resolve(data);
                }
            });
        });
    }
};

module.exports=Setting;
